import React, { useEffect, useRef, useState,forwardRef, useImperativeHandle}  from "react";
import { Modal,Input,Cascader,Radio, message,Button,Divider,Spin,Checkbox} from 'antd';
import {addSubject,importSubjects} from 'api/index'
import {CloudDownloadOutlined} from '@ant-design/icons'
import LlxForm from 'components/LlxForm/index'
import AudioUpload from 'components/Upload/audio'
import UploadPicture from 'components/Upload/index'
import SubjectWord from 'components/LlxPushData/subjectWord'
const {SHOW_CHILD}=Cascader
const defaultSubject = [
  {value:'1',label:'单选题', children: [{
    value: '5',
    label: '看图选文',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:true,
      optionsLength:2,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }, {
    value: '3',
    label: '看文选文',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:3,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  }, {
    value: '2',
    label: '听音选图',
    subject:{
      titleText:false,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:true,
    }
  }, {
    value: '10',
    label: '听音选音',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:true,
      optionsAudio:true,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '9',
    label: '看文选音',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:4,
      optionsText:false,
      optionsAudio:true,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '1',
    label: '听音选文',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:3,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '4',
    label: '看文选图',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:3,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:true,
    }
  }]
}, {
  value: '2',
  label: '判断题',
  children: [{
    value: '1',
    label: '听音选文',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:true,
      optionsLength:0,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  }]
}, {
  value: '6',
  label: '识读题',
  children: [{
    value: '7',
    label: '看文跟读', 
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:0,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
      answerHide:true
    }
  }]
}, {
  value: '7',
  label: '看图拼词',
  children: [{
    value: '5',
    label: '看图选文',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:true,
      optionsLength:5,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }]
}, {
  value: '4',
  label: '排序题',
  children: [{
    value: '3',
    label: '看文选文',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:5,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }, {
    value: '4',
    label: '看文选图',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:false,
      optionsLength:3,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:false,
    }
  }]
}, {
  value: '5',
  label: '连线题',
  children: [{
    value: '2',
    label: '听音选图',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:false,
      optionsLength:4,
      optionsText:false,
      optionsAudio:true,
      optionsPicture:false,
      optionTwo:true,
      fixed:true,
    }
  }, {
    value: '4',
    label: '看文选图',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:false,
      optionsLength:4,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      optionTwo:true,
      fixed:true,
    }
  }]
}
]

// 书籍
const defaultSubjectBook = [
  {value:'1',label:'单选题', children: [{
    value: '5',
    label: '看图选文',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:true,
      optionsLength:2,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }, {
    value: '3',
    label: '看文选文',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:3,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  }, {
    value: '2',
    label: '听音选图',
    subject:{
      titleText:false,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:true,
    }
  }, {
    value: '10',
    label: '听音选音',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:true,
      optionsAudio:true,
      optionsPicture:false,
      fixed:true,
    }
  }]
}, {
  value: '2',
  label: '判断题',
  children: [{
    value: '1',
    label: '听音选文',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:true,
      optionsLength:0,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  }]
} , {
  value: '4',
  label: '排序题',
  children: [{
    value: '4',
    label: '看文选图',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:false,
      optionsLength:3,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:false,
    }
  }]
}
]
// 主题课
const defaultSubjectTheme = [
  {value:'1',label:'单选题', children: [{
    value: '5',
    label: '看图选文',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:true,
      optionsLength:2,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }, {
    value: '3',
    label: '看文选文',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:3,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  }, {
    value: '2',
    label: '听音选图',
    subject:{
      titleText:false,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:true,
    }
  }, {
    value: '10',
    label: '听音选音',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:true,
      optionsAudio:true,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '9',
    label: '看文选音',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:4,
      optionsText:false,
      optionsAudio:true,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '1',
    label: '听音选文',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:3,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '4',
    label: '看文选图',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:3,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:true,
    }
  }]
}, {
  value: '2',
  label: '判断题',
  children: [{
    value: '1',
    label: '听音选文',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:true,
      optionsLength:0,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  }]
}, {
  value: '6',
  label: '识读题',
  children: [{
    value: '7',
    label: '看文跟读', 
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:0,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
      answerHide:true
    }
  }]
}, {
  value: '7',
  label: '看图拼词',
  children: [{
    value: '5',
    label: '看图选文',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:true,
      optionsLength:5,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }]
}, {
  value: '4',
  label: '排序题',
  children: [{
    value: '3',
    label: '看文选文',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:5,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }]
}, {
  value: '5',
  label: '连线题',
  children: [{
    value: '2',
    label: '听音选图',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:false,
      optionsLength:4,
      optionsText:false,
      optionsAudio:true,
      optionsPicture:false,
      optionTwo:true,
      fixed:true,
    }
  }, {
    value: '4',
    label: '看文选图',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:false,
      optionsLength:4,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      optionTwo:true,
      fixed:true,
    }
  }]
}
]
//  升级
const defaultSubjectPaper = [
  {value:'1',label:'单选题', children: [{
    value: '5',
    label: '看图选文',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:true,
      optionsLength:2,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }, {
    value: '3',
    label: '看文选文',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:3,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  }, {
    value: '2',
    label: '听音选图',
    subject:{
      titleText:false,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:true,
    }
  }, {
    value: '10',
    label: '听音选音',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:true,
      optionsAudio:true,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '1',
    label: '听音选文',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:3,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  }]
}, {
  value: '2',
  label: '判断题',
  children: [{
    value: '1',
    label: '听音选文',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:true,
      optionsLength:0,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  }]
}, {
  value: '6',
  label: '识读题',
  children: [{
    value: '7',
    label: '看文跟读', 
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:0,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
      answerHide:true
    }
  }]
}, {
  value: '7',
  label: '看图拼词',
  children: [{
    value: '5',
    label: '看图选文',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:true,
      optionsLength:5,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }]
}, {
  value: '4',
  label: '排序题',
  children: [{
    value: '3',
    label: '看文选文',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:5,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:false,
    }
  }]
}, {
  value: '5',
  label: '连线题',
  children: [{
    value: '2',
    label: '听音选图',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:false,
      optionsLength:4,
      optionsText:false,
      optionsAudio:true,
      optionsPicture:false,
      optionTwo:true,
      fixed:true,
    }
  }, {
    value: '4',
    label: '看文选图',
    subject:{
      titleText:false,
      titleAudio:false,
      titlePicture:false,
      optionsLength:4,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      optionTwo:true,
      fixed:true,
    }
  }]
}
]
// 定级
const defaultSubjectTestPaper =  [
  {value:'1',label:'单选题', children: [{
    value: '2',
    label: '听音选图',
    subject:{
      titleText:false,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:true,
    }
  }, {
    value: '10',
    label: '听音选音',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:2,
      optionsText:true,
      optionsAudio:true,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '9',
    label: '看文选音',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:4,
      optionsText:false,
      optionsAudio:true,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '1',
    label: '听音选文',
    subject:{
      titleText:true,
      titleAudio:true,
      titlePicture:false,
      optionsLength:3,
      optionsText:true,
      optionsAudio:false,
      optionsPicture:false,
      fixed:true,
    }
  },{
    value: '4',
    label: '看文选图',
    subject:{
      titleText:true,
      titleAudio:false,
      titlePicture:false,
      optionsLength:3,
      optionsText:false,
      optionsAudio:false,
      optionsPicture:true,
      fixed:true,
    }
  }]
}
]


const getSort = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']

const getIndex=(index) => {
  if (index === 0) return 'A'
  if (index === 1) return 'B'
  if (index === 2) return 'C'
  if (index === 3) return 'D'
  if (index === 4) return 'E'
  if (index === 5) return 'F'
  if (index === 6) return 'G'
  if (index === 7) return 'H'
  if (index === 8) return 'I'
  if (index === 9) return 'J'
  if (index === 10) return 'K'
  if (index === 11) return 'L'
  if (index === 12) return 'M'
  if (index === 13) return 'N'
  if (index === 14) return 'O'
  if (index === 15) return 'P'
  if (index === 16) return 'Q'
  if (index === 17) return 'R'
  if (index === 18) return 'S'
  if (index === 19) return 'T'
  if (index === 20) return 'U'
}

const NewSubject = forwardRef((props, ref) => {
  

useImperativeHandle(ref, () => ({
  options,
  setOption,
  check
}));

  const {subjectItem,form,dictionaries} = props

  const [options,setOption] = useState([])
  const [login,setLogin] = useState(false)
  const [check,setCheck] = useState(false)
  const [isModalVisible,setIsModalVisible] = useState(false)
  const [optionIndex,setOptionIndex] = useState(null)

  useEffect(()=>{
    setOption(Array(subjectItem.optionsLength).fill().map(i=>{
      return {
        "optionAudio": "",
				"optionName": "",
				"optionPicture": "",
				"optionValue": "",
				"pictureUrl": "",
        "audioUrl": "",
      }
    }))
  },[ subjectItem])

  const [refresh, setRefresh] = useState(false);

  useEffect(() => {
  refresh && setTimeout(() => setRefresh(false))
  }, [refresh])


  // 1.添加选项
  const pushOptionCount = () => {
    setOption(i=>[...i,{
      "optionAudio": "",
      "optionName": "",
      "optionPicture": "",
      "optionValue": "",
      "pictureUrl": "",
      "audioUrl": "",
    }])
  }
  // 2.移除选项
  const delOptionCount = () => {
    if(options.length===1){
      message.error('最后一条了,不可以删除了')
      return
    }
    const data = JSON.parse(JSON.stringify(options))
    data.splice(data.length-1,1)
    setOption(data)
  }
  // 3.随机排序
  const getSortAnswer = () => {
    if (!options||options.some(i => !i.pictureUrlOld)) {
      message.error('选项未补全，无法排序')
      return
    }
    setLogin(true)
    const data = options.map(i=>{
      return i.pictureUrlOld
    })
    data.sort(function(a, b) {
      return Math.random() - 0.5
    })

    const data1 = JSON.parse(JSON.stringify(options))

    data1.forEach((i, index) => {
      i.pictureUrl = data[index]
    })

    setOption(data1)

    let answer = []
    data1.forEach((i,j) => {
      answer.push(data.findIndex(x=>x===i.pictureUrlOld))
    })

    form.setFieldsValue({
      answerList:[{answer:answer.map(i=>getSort[i]).join(' ')}]
    })
    setTimeout(()=>{
      setLogin(false)
    },200)
  }
  // 4.看图拼词
  const spelling = (data) => {
    setLogin(true)

      const arr = Array(data.wordName.length).fill().map((i, index) => {
        return { optionName: data.wordName[index] }
      })


      const answerList = arr.sort(() => Math.random() - 0.5)
      const listTrue = []
      const listFalse = []
      const list = []
        
           
      setOption(answerList)

      answerList.forEach((i, y) => {
        Array(data.wordName.length).fill().map((i, index) => {
          return { optionName: data.wordName[index] }
        }).forEach((x) => {
          if (x.optionName === i.optionName) {
            if (listFalse.some(z => z.i === i.optionName)) {
              !listFalse.filter(z => z.i === i.optionName)[0].index.includes(y) && listFalse.filter(z => z.i === i.optionName)[0].index.push(y)
            } else {
              listFalse.push({ index: [y], i: i.optionName })
            }
          }
        })
      })
      answerList.forEach(i => {
        Array(data.wordName.length).fill().map((i, index) => {
          return { optionName: data.wordName[index] }
        }).forEach((x, y) => {
          if (x.optionName === i.optionName) {
            if (listTrue.some(z => z.i === i.optionName)) {
              !listTrue.filter(z => z.i === i.optionName)[0].index.includes(y) && listTrue.filter(z => z.i === i.optionName)[0].index.push(y)
            } else {
              listTrue.push({ index: [y], i: i.optionName })
            }
          }
        })
      })
      Array(data.wordName.length).fill().map((i, index) => {
        return { optionName: data.wordName[index] }
      }).forEach(i => {
        list.push(listFalse.filter(x => i.optionName === x.i).map(y => y))
      })
      list.forEach(i => {
        i[0].index.map(x => getIndex(+x))
      })
      var value = 1
      list.map(i => {
        return i[0].index.map(x => getIndex(+x))
      }).forEach(i => {
        value = i.length * value
      })

      var sarr = [[]]
      for (var i = 0; i < list.map(i => {
        return i[0].index.map(x => getIndex(+x))
      }).length; i++) {
        var tarr = []
        for (var j = 0; j < sarr.length; j++) {
          for (var k = 0; k < list.map(i => {
            return i[0].index.map(x => getIndex(+x))
          })[i].length; k++) {
            tarr.push(sarr[j].concat(list.map(i => {
              return i[0].index.map(x => getIndex(+x))
            })[i][k]))
          }
        }
        sarr = tarr
      }

      sarr.map(i => {
        const a = []
        i.forEach(x => {
          if (a.includes(x)) {
            return false
          } else {
            a.push(x)
          }
        })
        return i
      })
      const ssarr = []
      sarr.forEach(i => {
        const a = []
        let b = false
        i.forEach(x => {
          if (a.includes(x)) {
            b = true
          } else {
            a.push(x)
          }
        })
        if (!b) {
          ssarr.push(i)
        }
      })
      
      form.setFieldsValue({
        ...form.getFieldValue(),
        answerList:[{answer:ssarr.map(i => {
          return i.join(' ')
        }).join(',')}]
      })
      setTimeout(()=>{
        setLogin(false)
      },200)
  }
  // 5.导入单词
  const importData = (a,b) => {
    if(form.getFieldValue().type[0]==='7'){
      if(a.type!==1) {
        message.error('请导入全部')
        return
      }
      if(subjectItem.titlePicture){
        form.setFieldsValue({
          pictureUrl:a.pictureUrl
        })
      }

      spelling({wordName:a.wordName})

      setIsModalVisible(false)
      
      return
    }

    setLogin(true)
    if(optionIndex===null){
      switch(a.type){
        case 1:
         if(subjectItem.titleAudio){
           form.setFieldsValue({
             audioUrl:a.audioUrl
           })
         }
         if(subjectItem.titlePicture){
           form.setFieldsValue({
             pictureUrl:a.pictureUrl
           })
         }
         if(subjectItem.titleText){
           form.setFieldsValue({
            subjectName:a.wordName
           })
         }
        break
        case 2:
          if(subjectItem.titleAudio){
            form.setFieldsValue({
              audioUrl:a.audioUrl
            })
          }
        break
        case 3:
          if(subjectItem.titlePicture){
            form.setFieldsValue({
              pictureUrl:a.pictureUrl
            })
          }
        break
        default:
        return
      }
    } else {
      switch(a.type){
        case 1:
          setOption(value=>value.map((i,j)=>{
            if(j===optionIndex){
              if(subjectItem.optionsAudio){
                i.optionAudio=a.audioUrl
              }
              if(subjectItem.optionsPicture){
                i.optionPicture=a.pictureUrl
              }
              if(subjectItem.optionsText){
                i.optionName=a.wordName
              }
              if(subjectItem.optionTwo){
                i.pictureUrlOld=a.pictureUrl
             }
            }
            return i
          }))
        break
        case 2:
          setOption(value=>value.map((i,j)=>{
            if(j===optionIndex){
              if(subjectItem.optionsAudio){
                i.optionAudio=a.audioUrl
              }
            }
            return i
          }))
        break
        case 3:
          setOption(value=>value.map((i,j)=>{
            if(j===optionIndex){
              if(subjectItem.optionsPicture){
                i.optionPicture=a.audioUrl
              }
              if(subjectItem.optionTwo){
                i.pictureUrlOld=a.pictureUrl
             }
            }
            return i
          }))
        break
        default:
        return
      }
    }
    setTimeout(()=>{
      setLogin(false)
    },200)
    setIsModalVisible(false)
  }
  // 6.导入题干
  const importWordData = () => {
    setOptionIndex(null)
    setIsModalVisible(true)
  }
  // 7.导入选项
  const importWordOption = (value) => {
    setOptionIndex(value)
    setIsModalVisible(true)
  }
  // 输入框
  const checkText =(e,i)=>{
    i.optionName=e.target.value
    e.target.value=i.optionName
    setRefresh(true)
  }
  // 题干输入
  const checkTextTitle = (e) => {
    form.setFieldsValue({...form.getFieldValue,subjectName:e.target.value})
    setRefresh(true)
  }
  // 答案输入
  const checkTextAnswer = (e) => {
    form.setFieldsValue({...form.getFieldValue,answerList:[{answer:e.target.value}]})
    setRefresh(true)
  }
  const onChangeAnswer = (e) => {
console.log(e);
  }

  return form.getFieldValue().type?<div className="add-edit-subject">
    {/* 新建题目 */}
    <div className="subject">
    {/* 题干图片 */}
    {
      subjectItem.titlePicture?<div>
      <span>题干图片</span>
       <UploadPicture pictureUrl={form.getFieldValue().pictureUrl} setPhoto={(value)=>form.setFieldsValue({...form.getFieldValue(),pictureUrl:value})}></UploadPicture>
      </div>:''
    }
    {/* 题干音频 */}
    {
      subjectItem.titleAudio?<div>
      <span>题干音频</span>
      <AudioUpload  audio={form.getFieldValue().audioUrl} style={{marginLeft:'20px'}} setAudio={value=>form.setFieldsValue({...form.getFieldValue(),audioUrl:value})}></AudioUpload>
      </div>:''
    }
    {/* 题干文字 */}
    {
      subjectItem.titleText?<div>
      <span>题干文字</span>
      <Input  value={form.getFieldValue().subjectName} onChange={e=>checkTextTitle(e)} placeholder={'请输入题干文字'}></Input>
      </div>:''
    }
    {
      subjectItem.titlePicture||subjectItem.titleAudio?<div>
        <Button type={'primary'} size={'small'} onClick={importWordData}>单词库导入</Button>
      </div>:''
    }
   </div>
   <Divider></Divider>

     
   <Spin tip="Loading..." spinning={login}>

   {
     options.length?<div className="option">
     {
      options.map((i,j)=>{
       return <div key={j}>
           {/* 选项图片 */}
           {
             subjectItem.optionsPicture?<div>
               <span>选项图片</span>
               <UploadPicture pictureUrl={i.optionPicture} setPhoto={(value)=>i.optionPicture=value}></UploadPicture>
             </div>:''
           }
           {/* 选项音频 */}
           {
             subjectItem.optionsAudio?<div>
                <span>选项音频</span>
               <AudioUpload  audio={i.optionAudio} style={{marginLeft:'20px'}} setAudio={value=>i.optionAudio=value}></AudioUpload>
             </div>:''
           }
           {/* 选项文字 */}
           {
             subjectItem.optionsText?<div>
                <span>选项文字</span>
                <Input value={i.optionName} onChange={e=>checkText(e,i)} placeholder={'请输入选项文字'}></Input>
             </div>:''
           }
           {
                (subjectItem.optionsPicture||subjectItem.optionsAudio||subjectItem.optionsText) && form.getFieldValue().type[0]!=='7'?<div style={{marginTop:'20px'}}>
                <Button  shape="circle" icon={<CloudDownloadOutlined />}  type={'primary'} size={'large'} onClick={()=>importWordOption(j)}></Button>
              </div>:''
           }
       </div>
     })
     }
   </div>:''
   }

   {
     form.getFieldValue().type[0]==='5'?<div className="option">
     {
      options.map((i,j)=>{
       return <div key={j}>
           {/* 选项图片 */}
           {
             <div>
               <span>对应图片</span>
               <UploadPicture pictureUrl={i.pictureUrlOld} setPhoto={(value)=>i.pictureUrlOld=value}></UploadPicture>
             </div>
           }
       </div>
     })
     }
   </div>:''
   }

   {
    form.getFieldValue().type[0]==='5'?<div className="text-link" onClick={getSortAnswer}>
      随机打乱生成答案
    </div>:''
   }

   
{
    form.getFieldValue().type[0]==='5'? form.getFieldValue().type[0]==='5'?<div className="option">
    {
     options.map((i,j)=>{
      return <div key={j}>
          {/* 选项图片 */}
          {
            <div>
              <span>打乱后图片</span>
              <UploadPicture disabled pictureUrl={i.pictureUrl} setPhoto={(value)=>i.pictureUrl=value}></UploadPicture>
            </div>
          }
      </div>
    })
    }

   
  </div>:'':''
   }

   {
    !subjectItem.fixed?<div style={{marginTop:'20px'}}>
      <span  className="text-link" onClick={pushOptionCount}>添加选项</span>&nbsp;&nbsp;&nbsp;
      <span  className="text-link" onClick={delOptionCount}>移除选项</span>
    </div>:''
   }

    {
      subjectItem.answerHide||form.getFieldValue().type[0]==='1'||form.getFieldValue().type[0]==='2'||form.getFieldValue().type[0]==='5'?'':<div className="answer">
       <span className="text-color-red">*</span><span>请输入题目答案:</span><Input onChange={e=>checkTextAnswer(e)}  value={form.getFieldValue().answerList?form.getFieldValue().answerList[0].answer:''} placeholder="请输入答案,存在多个答案以英文逗号分隔,例如 A B C,B A C" ></Input>
    </div>
    }

{
       form.getFieldValue().type[0]==='1'?<div className="answer">
       <span className="text-color-red">*</span><span>请选择答案:</span>
       <Radio.Group onChange={e=>form.setFieldsValue({...form.getFieldValue,answerList:[{answer:e.target.value}]})} defaultValue={form.getFieldValue().answerList?form.getFieldValue().answerList[0].answer:'A'}>
       {
         getSort.slice(0,options.length)?.map(i=>{
           return  <Radio value={i} key={i}>{i}</Radio>
         })
       }
      </Radio.Group>
    </div>:''
    }

    {
      form.getFieldValue().type[0]==='2'?<div style={{marginTop:'20px'}}>
       <span className="text-color-red">*</span> 请选择题目答案: <Radio.Group onChange={e=>form.setFieldsValue({...form.getFieldValue,answerList:[{answer:e.target.value}]})}>
      <Radio value={'true'}>正确</Radio>
      <Radio value={'false'}>错误</Radio>
    </Radio.Group>
    </div>:''
    }

   <div>
    <Checkbox style={{margin:'10px 0 0 900px'}} onChange={(e)=>setCheck(e)}>同时添加到题库</Checkbox>
   </div>


  </Spin>

  <SubjectWord isModalVisible={isModalVisible} setIsModalVisible={setIsModalVisible} dictionaries={dictionaries} importData={importData}></SubjectWord>

</div>:<div style={{marginLeft:'550px'}}>请先选择题目模板 !</div>
})



const AddOrEditSubject = (props) => {

  const {isModalVisible, setIsModalVisible,form,dictionaries,globalOriginType,paperBookId,sort,type} = props

  const [examination,setExamination]=useState([])
  const [subjectTypeList,setSubjectTypeList]=useState([])

  const [subjectItem,setSubjectItem]=useState({})

  const options = useRef()


  const newItem = [
  {label: '题型', name: 'type', span: 6 ,rules:[
    {
      required: true,
      message: '请输入题目模板'
    },
  ],render:()=>{
    return <Cascader  options={subjectTypeList} onChange={onChange} placeholder={'请选择对应的题目模板'}/>
  }},
    {label: '标签', name: 'label' , span: 6 ,render: () => {
      return <Cascader  maxTagCount="responsive" showCheckedStrategy={SHOW_CHILD} fieldNames={{value:'id',label:'labelName'}} options={dictionaries.Label} multiple placeholder="请选择标签" />
  }},
  {label: '背景音乐', name: 'backgroundMusic', span: 7 ,render:()=>{
    return  <AudioUpload  audio={form.getFieldValue().backgroundMusic} style={{marginLeft:'20px'}} setAudio={value=>form.setFieldsValue({...form.getFieldValue(),backgroundMusic:value})}></AudioUpload>
  }},
  {label: '引导文字',type:'input',name: 'guideText', span: 6},
  {label: '引导语音', name: 'guideAudio', span: 6 ,render:()=>{
    return  <AudioUpload  audio={form.getFieldValue().guideAudio} style={{marginLeft:'20px'}} setAudio={value=>form.setFieldsValue({...form.getFieldValue(),guideAudio:value})}></AudioUpload>
  }},
  {label: '解析文字',type:'input',name: 'analysisText', span: 6},
  {label: '解析语音', name: 'analysisAudio', span: 6 ,render:()=>{
    return  <AudioUpload  audio={form.getFieldValue().analysisAudio} style={{marginLeft:'20px'}} setAudio={value=>form.setFieldsValue({...form.getFieldValue(),analysisAudio:value})}></AudioUpload>
  }},
  {label: '考点种类', name: 'examinationTypeId' , span: 6 ,rules:[
    {
      required: true,
      message: '请输入选择考点种类'
    },
  ],render: () => {
    return <Cascader  maxTagCount="responsive" onChange={onExamination}  showCheckedStrategy={SHOW_CHILD} fieldNames={{value:'id',label:'dictValue'}} options={dictionaries.SubjectType} multiple placeholder="请选择种类" />
}},
{label: '', name: 'examinationbbb' , span: 18 ,render: () => {
  return <div className="examination-form">
  {examination?.map(i=>{
    return <div key={i.examinationTypeId} >
      {i.dictValue}:
    <Input onChange={e=>i.examination=e.target.value} placeholder={'请输入考点'}></Input>
    </div>
  })}
  </div>
}}
]


useEffect(()=>{
  if(!isModalVisible) return

  if(type==='IR_BOOK'){
    setSubjectTypeList(defaultSubjectBook)
  } else if(type==='THEME'){
    setSubjectTypeList(defaultSubjectTheme)
  }else if(type==='PAPER'){
    setSubjectTypeList(defaultSubjectPaper)
  }else if(type==='TEST'){
    setSubjectTypeList(defaultSubjectTestPaper)
  } else {
    setSubjectTypeList(defaultSubject)
  }
  return ()=>form.resetFields()
}, [isModalVisible]) // eslint-disable-line react-hooks/exhaustive-deps


  // 返回
const handleOk = async () => {

   const {label,id,examinationTypeId,type,guideAudio,guideText,subjectName,pictureUrl,audioUrl,analysisAudio,analysisText,backgroundMusic}=form.getFieldValue()
    


    // 发送请求
    form.validateFields().then(async()=>{


      let answerList = form.getFieldValue().answerList

      const value = answerList[0].answer.indexOf(',')!==-1
    
      const value1 = answerList[0].answer.indexOf(',')!==-1?answerList[0].answer.split(',').filter(i=>i).map(i=>{
        return i.split('').filter(x=>x).join(' ')
      }).join(','):''
  
     if(type[0]==='7' && value){
      //  看图拼词
      answerList=[
        {
          answer:value1
        }
      ]
     } else if(type[0]==='4'&&type[1]==='3'&&value){
     // 排序题
     answerList=[
       {
        answer:value1
       }
      ]
     } 

    if(type[0]!=='5'&&type[0]!=='7'){
      if(!answerList||!answerList[0]||!answerList[0].answer){
        message.error('检测到你未填写答案')
        return
      }
    }
    

    if(type[0]==='1'){
      if(answerList[0].answer.trim().length!==1){
        message.error('检测到你答案可能有误，检查后再提交')
        return
      }
    }
 
      if(subjectItem.titleText&&!subjectName){
        message.error('是不是题目文字未填写？？？？')
        return
      }
      if(subjectItem.titleAudio&&!audioUrl){
        message.error('是不是题目音频未上传？？？？')
        return
      }
      if(subjectItem.titlePicture&&!pictureUrl){
        message.error('是不是题目图片未上传？？？？')
        return
      }

      const params = {
        analysisAudio,
        analysisText,
        answerList,
        audioUrl,
        backgroundMusic,
        examination: examination,
        examinationType:examinationTypeId.map(i=>{
          return {
            examinationTypeId:i[0],
            grammarId:i[1]?i[1]:''
          }
        }),
        globalOriginType: globalOriginType,
        guideAudio,
        guideText,
        id,
        labels: label?.map(i=>{
          return {id:i[1]}
        }),
        options: options.current.options,
        paperBookId: paperBookId,
        pictureUrl,
        sort: sort,
        subjectName,
        subjectType: type[1],
        type: type[0],
      }

      const res = await addSubject(params)

     if(props.getSubjectId){
       props.getSubjectId(res.result)
     } else {
      message.success('新建成功')
     }

       
      if(options.current.check){
        // 添加到题库
        await importSubjects({
          subjectId: [res.result],
          globalOriginType: globalOriginType,
          importId:paperBookId
        })
       setTimeout(()=>{
        message.success('导入成功')
       },200)
      }

      setIsModalVisible(false);
      props.getInfoFn()
    })
  }
  const onCancel = () => {
    setIsModalVisible(false);
  };
// 1. 切换题型
  const onChange = (value) => {
    console.log(value);
    form.setFieldsValue({pictureUrl:'',audioUrl:'',subjectName:'',options:[],answerList:[{answer:''}]})
    options.current.setOption([])
    setSubjectItem(subjectTypeList.filter(i=>i.value===value[0])[0].children.filter(i=>i.value===value[1])[0].subject)
  }
 // 2.考点输入
 const onExamination = (value) => {
  const data = value.map(i => {
    return i[i.length - 1]
  })
  const data1 = []
  // eslint-disable-next-line array-callback-return
  dictionaries.SubjectType.filter(i=> {
    if (i.children) {
      i.children.forEach(item => {
        if (data.includes(item.id)) {
          data1.push(item)
        }
      })
    } else {
      if (data.includes(i.id)) {
        data1.push(i)
      }
    }
  })

   setExamination(data1.map(i=>{
    return {examinationTypeId:i.id,examination:'',dictValue:i.dictValue}
  }))
 }
   
  return   <Modal wrapClassName={'subject-add'}  width={1200} title={'新增题目'} destroyOnClose visible={isModalVisible} onOk={handleOk} onCancel={onCancel}>
      

      <LlxForm preserve={true} form={form} labelCol={7} formItem={newItem} />

      

      <NewSubject dictionaries={dictionaries} ref={options} form={form} subjectItem={subjectItem}></NewSubject>



   </Modal>

}
export default AddOrEditSubject